# THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: 
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),  
# to deal in the Software without restriction, including without limitation  
# the rights to use, copy, modify, merge, publish, distribute, sublicense,  
# and/or sell copies of the Software, and to permit persons to whom the  
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in 
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS IN THE SOFTWARE.

# events.py
# written by: 
#     Jan Jokela <jan@devion.org>

import sys
import pygame
from pygame.locals import *
import eye

class EventHandler(object):
    """
    If you want to subscrive a class to the event handler
    use eventhandler.subscribe(class) and subsequently the 
    unsubscribe method. The eventhandler will pass a list
    of events to the handle_events(self) method in the subscribed class
    There is not an abstracted class for events. every event object that
    has a public .type should be ok (look at fluidEvent)
    """
    def __init__(self):
        self.clients = []
    def subscribe(self, client):
        self.clients.append(client)
    def unsubscribe(self, client):
        self.clients.remove(client)
    def watch(self):
        self.events = []
        # Pygame events
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            if event.type is KEYDOWN:
                if event.key == K_ESCAPE: sys.exit()
            if event.type == MOUSEBUTTONDOWN:
                eye.mousehandler.set_active()
            if event.type == pygame.VIDEORESIZE:
                eye.config_dimensions(event.w, event.h)
            self.events.append(event)
              
        for event in eventqueue.get():
            self.events.append(event)
        eventqueue.destroy()
                     
        # Lets return events to clients
        for client in self.clients:
            client.handle_events(self.events)
    def get_clients(self):
        for client in self.clients:
            print client     
    def clear(self):
         self.events = []

class MouseHandler(object):
    def __init__(self):
        eye.actionhandler.subscribe(self)
        self.mouse_active = False
        self.mouse_timer = 0
    def execute(self):
        movement = pygame.mouse.get_rel()
        if movement[0] != 0 or movement[1] != 0:
            if not self.mouse_active:
                self.mouse_active = True
                pygame.mouse.set_visible(True)  
                eventqueue.add(FluidEvent("mouse_active"))
            else: self.mouse_timer = 0 
        elif movement[0] == 0 and movement[1] == 0:
            if self.mouse_active:
                if self.mouse_timer < 200:
                    self.mouse_timer+=1
                else:
                    self.mouse_timer = 0 
                    self.mouse_active = False
                    pygame.mouse.set_visible(False)  
                    eventqueue.add(FluidEvent("mouse_inactive"))  
    def set_active(self):
        if not self.mouse_active:
            self.mouse_active = True
            pygame.mouse.set_visible(True)  
            eventqueue.add(FluidEvent("mouse_active"))
        else: self.mouse_timer = 0         
    def active(self):
        if self.mouse_active: return True
        else: return False
        
class EventQueue(object):
    def __init__(self):
        self.events = []
    def add(self, event):
        self.events.append(event)
    def get(self):
        return self.events
    def destroy(self):
        self.events = []

class ActionHandler(object):
    def __init__(self):
        self.clients = []
    def subscribe(self, client):
        self.clients.append(client)
    def unsubscribe(self, client):
        eventqueue.add(FluidEvent("action_finished", client))
        self.clients.remove(client)
    def watch(self):
        for client in self.clients:
            client.execute()

class FluidEvent(object):
    def __init__(self, type, key=None):
        self.type = type
        self.key = key
        
class OtherEvent(FluidEvent):
    def __init__(self, type, key=None):
        self.type = type
        self.key = key
        
eventqueue = EventQueue()
        